﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlLibary
{
    /// <summary>
    /// 显示颜色的列
    /// </summary>
    public class ColorColumn : DataGridViewColumn
    {
        public ColorColumn() :
            base(new ColorCell())
        {
            ReadOnly = true;
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if ((value != null) && !(value is ColorCell))
                {
                    throw new InvalidCastException("值必须为ColorCell");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class ColorCell : DataGridViewTextBoxCell
    {
        #region Init

        public ColorCell()
        {

        }

        #endregion

        Rectangle funcRectangle;

        #region 绘制节点

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            if (value != null)
            {
                if (value is DBColor)
                {
                    DBColor color = value as DBColor;

                    string str = !string.IsNullOrEmpty(color.ColorName) ? color.ColorName : string.Format("{0},{1},{2}", color.R, color.G, color.B);

                    funcRectangle = new Rectangle(cellBounds.Left + 5, cellBounds.Top + 2, 20, cellBounds.Height - 5);

                    int width = 28;

                    Rectangle canvas = new Rectangle(cellBounds.Left, cellBounds.Top, width, cellBounds.Height);

                    //绘制单元格背景
                    using (Brush bruCanvas = new SolidBrush(this.Selected ? cellStyle.SelectionBackColor : cellStyle.BackColor))
                    {
                        graphics.FillRectangle(bruCanvas, canvas);
                    }

                    //绘制边框
                    using (Pen gridPen = new Pen(DataGridView.GridColor))
                    {
                        graphics.DrawLine(gridPen, canvas.Left, canvas.Location.Y + canvas.Height - 1, canvas.Right, canvas.Location.Y + canvas.Height - 1);

                        //颜色区边框
                        Rectangle borderRectangle = new Rectangle(cellBounds.Left + 4, cellBounds.Top + 1, 21, cellBounds.Height - 4);

                        //绘制颜色边框
                        graphics.DrawRectangle(gridPen, borderRectangle);
                    }

                    using (Brush bruBK = new SolidBrush(Color.FromArgb(color.R, color.G, color.B)))
                    {
                        graphics.FillRectangle(bruBK, funcRectangle);
                    }


                    cellBounds.X += width;

                    cellBounds.Width -= width;

                    //绘制颜色Name
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, str, str, errorText, cellStyle, advancedBorderStyle, paintParts);

                }
                else
                {
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
                }
            }
            else
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            }
        }

        #endregion


        #region 点击 Cell

        protected override void OnMouseDoubleClick(DataGridViewCellMouseEventArgs e)
        {
            Rectangle rect = DataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);
            Point pt = new Point(rect.Left + e.Location.X, rect.Top + e.Location.Y);
            if (rect.Contains(pt))
            {
                ColorDialog clr = new ColorDialog();
                if (clr.ShowDialog() == DialogResult.OK)
                {
                    Color c = clr.Color;

                    Value = new DBColor("", c.R, c.G, c.B);
                    this.DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseDoubleClick(e);
        }

        //protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
        //{
        //    Rectangle rect = DataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);
        //    Point pt = new Point(rect.Left + e.Location.X, rect.Top + e.Location.Y);
        //    if (rect.Contains(pt))
        //    {
        //        ColorDialog clr = new ColorDialog();
        //        if (clr.ShowDialog() == DialogResult.OK)
        //        {
        //            Color c = clr.Color;

        //            Value = new DBColor("", c.R, c.G, c.B);
        //            this.DataGridView.InvalidateCell(this);
        //        }
        //    }
        //    base.OnMouseDown(e);
        //}


        #endregion

    }
}
